import { ref } from 'vue'
import {
	useCurrentInstance
} from '@/common/hooks'
import { getLocation, defaultLatitude, defaultLongitude } from '@/common/utils/map/index'
import { useI18n} from "vue-i18n";


export const useUserLocation = (cb?:Function) => {

	const ctx = useCurrentInstance()
	const { t } = useI18n()
	
	// const latitude = ref(defaultLatitude)
	// const longitude = ref(defaultLongitude)
	
	const _getLocation = async (isRefresh?:boolean):ReturnType<typeof getLocation> => {
		let userGeolocation = uni.getStorageSync('userGeolocation')
		if (userGeolocation&&!isRefresh) {
			// latitude.value = userGeolocation.latitude
			// longitude.value = userGeolocation.longitude
			return userGeolocation 
		} else {
			const res = await getLocation(t('nearby.openGPS'),t('nearby.GPSErr'))
			// latitude.value = res.latitude
			// longitude.value = res.longitude
			return res
		}
	}

	
	return {
		// latitude,
		// longitude,
		getLocation:_getLocation
	}
}